//
//  SuggestViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/10/29.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "SuggestViewController.h"
#import "IWTextView.h"
#import "WeChatLoginViewController.h"
@interface SuggestViewController ()
@property (nonatomic, strong)IWTextView * textField;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSString * phoneString;
@property (nonatomic, strong)NSString * tokenString;
- (void)ifLogin;
@end

@implementation SuggestViewController(networking)
- (void)submitSuggestData{
    NSDictionary * dic = @{
                           @"phone":self.phoneString,
                           @"token":self.tokenString,
                           @"content":self.textField.text
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"messageiosApi/feedback" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else if ([[NSString stringWithFormat:@"%@", responseObject[@"sign"]] isEqualToString:@"400"]) {
            [self ifLogin];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation SuggestViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    if ([[UserModel sharedInstanced].token isEqualToString:@""] || [UserModel sharedInstanced].token == nil || [[UserModel sharedInstanced].token length] == 0 || [[UserModel sharedInstanced].mobile isEqualToString:@""] || [UserModel sharedInstanced].mobile == nil || [[UserModel sharedInstanced].mobile length] == 0) {
        self.tokenString = @"";
        self.phoneString = @"";
    }else{
        self.tokenString = [UserModel sharedInstanced].token;
        self.phoneString = [UserModel sharedInstanced].mobile;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"意见反馈";
    
    self.view.backgroundColor = getColor(mainColor);
    [self.view addSubview:self.tableView];
    
    UIButton * submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25/WIDTH_6_SCALE, 25/WIDTH_6_SCALE)];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    submitBtn.titleLabel.font = DEF_FontSize_14;
    [submitBtn addTarget:self action:@selector(submitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:submitBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (IWTextView *)textField{
    if (!_textField) {
        _textField = [[IWTextView alloc] initWithFrame:CGRectMake(10/WIDTH_6_SCALE, 10/WIDTH_6_SCALE, SCREEN_WIDTH - 20/WIDTH_6_SCALE, 200/WIDTH_6_SCALE)];
        _textField.placeholder = @"感谢你的宝贵意见及建议";
        _textField.backgroundColor = [UIColor whiteColor];
    }return _textField;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 260/WIDTH_6_SCALE)];
        _headerView.backgroundColor = getColor(mainColor);
        [_headerView addSubview:self.textField];
    }return _headerView;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = getColor(mainColor);
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _tableView.tableHeaderView = self.headerView;
    }return _tableView;
}
#pragma mark - action
- (void)submitBtnClick:(UIButton *)sender{
    if (self.textField.text == nil || [self.textField.text length] == 0 || [self.textField.text isEqualToString:@""]) {
        ShowMessage(@"请填写具体内容");
    }else{
        [self submitSuggestData];
    }
}
- (void)ifLogin{
    WeChatLoginViewController * vc = [[WeChatLoginViewController alloc] init];
    UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:vc];
    vc.backHomePageBlock = ^{
        [vc dismissViewControllerAnimated:YES completion:nil];
        [self.tabBarController setSelectedIndex:0];
        [self.navigationController popToRootViewControllerAnimated:YES];
    };
    [self presentViewController:navi animated:YES completion:nil];
}
@end
